<?php

namespace app\admin\controller;
use app\base\controller\Base;
use think\Request;
use think\Cache;
use think\Config;
use think\Session;



//资金流水日志
class Recharge extends Base
{
    public  $model;

    public function __construct()
    {
   $this->model = model("UsersRecharge");
        parent::__construct();

    }
    /**
     * @return \think\response\Json  询价历史
     */
    public function index()
    {//
        //搜索条件 时间 用户号码
        $where = [];
        $t = 60*60*24-1;
        if(isset($this->start_time)||isset($this->end_time))
        {
            if(isset($this->start_time)&&isset($this->end_time)&&($this->start_time==$this->end_time)){

                $this->end_time = $this->start_time;
                $where['a.created_at']=['between',[strtotime($this->start_time),strtotime($this->end_time)+$t]];
            }else{
                $where['a.created_at']=['between',[isset($this->start_time)?strtotime($this->start_time):strtotime("1970/1/1"),isset($this->end_time)?strtotime($this->end_time)+$t:strtotime("3030/11/1")]];
            }


        }
        if(isset($this->mobile)){
            $where['u.mobile']  = $this->mobile;
        }

        if(isset($this->status)){
            $where['a.status']  = $this->status;
        }
        if(isset($this->type)){
            $where['a.type']  = $this->type;
        }




        //时间的处理

     $res = $this->model->recharge_page($where);

     return   resJson(1,$this->ecLang("query"),$res['res'],$res['pageInfo']);
    }


    /**
     *
     */
    public function reject(){

        $res = $this->model->where(["id"=>$this->id,"status"=>0])->find();

        if(empty($res)){
            return failJson('参数异常');
        }
        $result = $this->model->save(["status"=>2],["id"=>$this->id]);
   
        if(!$result){
            return failJson("驳回失败");
        }
        return sucessJson("驳回成功");
    }

    /**
     * 更改当前充值状态  审核充值通过
     */
    public function changestatus(){

        $res = $this->model->where(["id"=>$this->id,"status"=>0,"type"=>1])->find();

        if(empty($res)){

            return   failJson($this->ecLang("failstatus"));
        }
        if($res['status']!=0){
            return   failJson("该订单未在审核列表中");//审核中
        }

        if($res['agent_id']&&$res['staff_id']){
            //员工提现
            $result =  $this->model->YuanGongCz($res);
            if(!$result['status']){
                return   failJson("代理商员工提现失败");
            }

            return sucessJson("代理商员工提现成功");

        }

        if($res['agent_id']&&!$res['staff_id']){
            //代理商提现
            $result =  $this->model->dailiCz($res);
            if(!$result['status']){

                return   failJson("代理商提现失败");
            }
            return sucessJson("代理商提现成功");
        }

       $UserModel = new \app\np\model\UsersInvest();
       $Users =  $UserModel->where("user_id",$res['user_id'])->find();
        $result =  $this->model->changestatus($res);
        $content = "用户{$Users['user_name']} 充值单号:{$res['pay_no']}";
        if(!$result){

            return   failJson($content.","."充值失败");
        }

        return   sucessJson($content.","."充值成功");
    }


    /**agentwithdraw
     * 确认提现
     */
    public function changetx(){
        $res = $this->model->where(["id"=>$this->id,"status"=>0,"type"=>2])->find();
        if(empty($res)){
            return   failJson($this->ecLang("failstatus"));
        }
        if($res['status']!=0){
            return   failJson("该订单未在审核列表中");//审核中
        }

        if($res['agent_id']&&$res['staff_id']){
                    //员工提现
            $result =  $this->model->YuanGongTx($res);
            if(!$result['status']){

                return   failJson("代理商员工提现失败");
            }

            return sucessJson("代理商员工提现成功");

        }

        if($res['agent_id']&&!$res['staff_id']){
                //代理商提现
            $result =  $this->model->dailiTx($res);
            if(!$result['status']){

                return   failJson("代理商提现失败");
            }
            return sucessJson("代理商提现成功");
        }

        $UsersMoney= new \app\np\model\UsersMoney; //用户资金

        $money = $UsersMoney->where("user_id",$res['user_id'])->find();
        if($money['advisable']<$res['deposit']){
            return   failJson("余额不足,无法提现");//审核中

        }

        $UserModel = new \app\np\model\UsersInvest();
        $Users =  $UserModel->where("user_id",$res['user_id'])->find();
        $content = "用户{$Users['user_name']} 充值单号:{$res['pay_no']}";

        $result =  $this->model->changetx($res);
        if(!$result['status']){
            w_s("resnd",["content"=>$content.","."提现失败","user_id"=>$res['user_id']]);
           // addMessage($res['user_id'],"提现单号：{$res['pay_no']}，提现失败");

            return   failJson($result['msg']);
        }
        w_s("resnd",["content"=>$content.","."提现成功","user_id"=>$res['user_id']]);




        //用户提现成功
        //搜索用户手机号码
        $users =  new \app\out\model\Users;
       $userlist =  $users->where(['id'=>$res['user_id']])->find();
       /* $content = ":尊敬的用户,您提现的{$res['deposit']}元已审核成功";
        sedMessageAll($userlist['mobile'],$content);*/
        sedMessageAll($userlist['mobile'],"txmessage",$res['deposit']);
        return   sucessJson($this->ecLang("sucess"));
    }




    //代理商充值
    public function agentrecharge(){
        //身份验证通过 银行卡绑定

        $UsersRecharge = model("UsersRecharge");
        $Cstatic = controller('base/Cstatic');
       $agent_id =  $Cstatic->get("agent_id");
        if(empty($agent_id)){
            return   failJson("非代理商不能充值");
        }
        $data = [
            "deposit"=>$this->deposit,
            "agent_id"=>$agent_id,
            // "bank_id"=>$this->bank_id,
            "type"=>1,
            "pay_no"=>ordersn()
        ];
        $res =  $UsersRecharge->save($data);
        if(!$res){
            return   failJson("充值失败");
        }
        if(isset($urls)){
            $d = ["url"=>$urls];
        }else{
            $d = [];
        }
        return sucessJson("充值成功",$d);
        //充值
    }




    //用户提现
    public function agentwithdraw(){
        //身份验证通过 银行卡绑定

        $UsersRecharge = model("UsersRecharge");

        $staffModel = new \app\admin\model\AdminStaff();
        $resultStaff = $staffModel->where("id",$this->sidRows['id'])->find();
        if($resultStaff['status']==0){
            return failJson("改用户已禁用提现,请联系总管理员");
        }


        $sidRows=  $this->sidRows;

        $data = [
            "deposit"=>$this->deposit,
            "agent_id"=>$sidRows['agent_id'],
            "staff_id"=>$this->admin_user_id,
            "type"=>2,
        ];
        if($sidRows['is_manger']){
                    //是否是代理商管理员
            unset($data['staff_id']);//提现代理商

     $deposit =  $this->model->where("status",0)->where(" (agent_id = {$sidRows['agent_id']} and staff_id is null) ")->sum("deposit");
     $sumDeposit  = $deposit['deposit'];
            if(!$sumDeposit){
                $sumDeposit = 0;
            }
            //代理商提现
            $AdminAgent = new \app\admin\model\AdminAgent();
            $AdminAgentResult =  $AdminAgent->where("id",$sidRows['agent_id'])->find();
            if(bcsub($AdminAgentResult['money'],bcadd($this->deposit,$sumDeposit),2)<0){
                    return failJson("代理商余额不足,无法提现");
            }



        }else{

            $deposit =  $this->model->where("status",0)->where("staff_id",$sidRows['staff_id'])->sum("deposit");
            $sumDeposit  = $deposit['deposit'];
            if(!$sumDeposit){
                $sumDeposit = 0;
            }
            //员工提现
            $AdminStaff = new \app\admin\model\AdminStaff();
            $AdminStaffResult =  $AdminStaff->where("id",$this->admin_user_id)->find();
            if(bcsub($AdminStaffResult['money'],bcadd($this->deposit,$sumDeposit),2)<0){

                return failJson("代理商员工余额不足,无法提现");
            }
        }






        //否则为员工提现


        $res =  $UsersRecharge->save($data);
        if(!$res){
            return   failJson(" 提现申请失败");
        }
        return sucessJson("提现申请成功");
        //"status" deposit user_id type
        //充值


    }







}
